package com.fanfanet.lesson13;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Inquirer {
    //查询条件集合
    private List<BooleanExpression> expressions;

    public Inquirer(){
        this.expressions = new ArrayList<>();
    }

    public Inquirer putExpression(BooleanExpression expression){
        //添加到条件集合
        expressions.add(expression);
        return this;
    }

    public Predicate buidelQuery(){
        //第一个查询条件对象
        BooleanExpression be = null;
        //遍历所有查询条件，以第一个开始and
        for (int i = 0; i<expressions.size(); i++){
            if (i == 0){
                be = expressions.get(i);
            }else {
                be = be.and(expressions.get(i));
            }
        }
        return be;
    }

    public <T> List<T> iteratorToList(Iterable<T> iterable){
        List<T> returnList = new ArrayList<T>();
        Iterator<T> iterator = iterable.iterator();
        while (iterator.hasNext()){
            returnList.add(iterator.next());
        }
        return returnList;
    }
}
